/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.maixiami;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 叶勤勇
 */
public class TaobaoUserAccessor {
    private EntityStore store;
    PrimaryIndex<Long, TaobaoUserEntity> priIndex;
    SecondaryIndex<String, Long, TaobaoUserEntity> nickIndex;
    SecondaryIndex<String, Long, TaobaoUserEntity> stateIndex;
    SecondaryIndex<String, Long, TaobaoUserEntity> statecityIndex;
    SecondaryIndex<String, Long, TaobaoUserEntity> shopTitleIndex;
    SecondaryIndex<Long, Long, TaobaoUserEntity> cidsIndex;
    
    public TaobaoUserAccessor(EntityStore store) throws DatabaseException {
        this.store = store;
        priIndex = store.getPrimaryIndex(Long.class, TaobaoUserEntity.class);
        nickIndex = store.getSecondaryIndex(priIndex, String.class, "nick");
        stateIndex = store.getSecondaryIndex(priIndex, String.class, "state");
        statecityIndex = store.getSecondaryIndex(priIndex, String.class, "statecity");
        shopTitleIndex = store.getSecondaryIndex(priIndex, String.class, "shopTitle");
        cidsIndex = store.getSecondaryIndex(priIndex, Long.class, "cids");
    }
    
    public void putTaobaoUserEntity(TaobaoUserEntity user) throws DatabaseException {
        priIndex.put(user);
    }
    
    public TaobaoUserEntity getTaobaoUserEntityById(Long userId) throws DatabaseException {
        return priIndex.get(userId);
    }

    public TaobaoUserEntity getTaobaoUserEntityByNick(String nick) throws DatabaseException {
        return nickIndex.get(nick);
    }
    
    public List<TaobaoUserEntity> getTaobaoUserEntityByState(String state) throws DatabaseException {
        List<TaobaoUserEntity> users = new ArrayList<TaobaoUserEntity>();
        EntityCursor<TaobaoUserEntity> cursor = null;
        try {
            cursor = stateIndex.subIndex(state).entities();
            for(TaobaoUserEntity user : cursor) {
                users.add(user);
            }
        } finally {
            if(cursor != null)
                cursor.close();
        }
        return users;
    }

    public List<TaobaoUserEntity> getTaobaoUserEntityByStatecity(String statecity) throws DatabaseException {
        List<TaobaoUserEntity> users = new ArrayList<TaobaoUserEntity>();
        EntityCursor<TaobaoUserEntity> cursor = null;
        try {
            cursor = statecityIndex.subIndex(statecity).entities();
            for(TaobaoUserEntity user : cursor) {
                users.add(user);
            }
        } finally {
            if(cursor != null)
                cursor.close();
        }
        return users;
    }
    
    public List<TaobaoUserEntity> getTaobaoUserEntityByShopTitle(String shopTitle) throws DatabaseException {
        List<TaobaoUserEntity> users = new ArrayList<TaobaoUserEntity>();
        EntityCursor<TaobaoUserEntity> cursor = null;
        try {
            cursor = shopTitleIndex.subIndex(shopTitle).entities();
            for(TaobaoUserEntity user : cursor) {
                users.add(user);
            }
        } finally {
            if(cursor != null)
                cursor.close();
        }
        return users;
    }
    
    public List<TaobaoUserEntity> getTaobaoUserEntityByCid(Long cid) throws DatabaseException {
            List<TaobaoUserEntity> users = new ArrayList<TaobaoUserEntity>();
        EntityCursor<TaobaoUserEntity> cursor = null;
        try {
            cursor = cidsIndex.subIndex(cid).entities();
            for(TaobaoUserEntity user : cursor) {
                users.add(user);
            }
        } finally {
            if(cursor != null)
                cursor.close();
        }
        return users;
    }

}
